package com.wooboo.server.ext.action.redis;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.SysLogger;
import com.wooboo.server.ext.data.Traffic;
import com.wooboo.server.ext.data.UserFinance;
import com.wooboo.server.ext.service.DbService;
import com.wooboo.server.ext.util.DateUtil;
import com.wooboo.server.ext.util.HttpClientUtils;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;

import net.sf.json.JSONObject;


/**
 * @author www.wooboo.com.cn
 * 流量汇企业信息注册申请
 */
public class Add extends ActionBase{
	
	public Integer advertiserId;//广告主
	public String company_name;//公司名称
	public String city_name;//城市名称
	public String contact_mobile;//电话
	public String contact_email;//邮件
	
	
	public Add(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		company_name=params.getFieldValue("company_name");
		advertiserId = NumberUtil.getInteger(params.getFieldValue("advertiserId"));
		if(StringUtil.isEmpty(company_name,advertiserId)){
		    throw ApiError.Type.INVALID_PARAM.toException();
		}
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		DbService dbService = new DbService();
		UserFinance userfinance=dbService.findUserFinanceById(advertiserId);
		if(null==user){
			throw ApiError.Type.INVALID_PARAM.toException("账户信息不存在。");
		}
		
		String dateStr = DateUtil.getFormatDate(new Date(), "yyyyMMddHHmmss");
		Map<String, String> params = new HashMap<>();
		params.put("portalType", Traffic.PORTAL_TYPE);//门户类型 
		params.put("portalID", Traffic.PORTAL_ID);
		params.put("transactionID",dateStr+StringUtil.createId(8));//交易唯一编码：yyyMMddHHmmss+8位唯一序号
		params.put("method", Traffic.Method.companyRegisterApply.toString());//方法
		params.put("company_name", company_name);//公司
		params.put("create_time", dateStr);//时间戳
		params.put("contact_mobile", contact_mobile);
		params.put("contact_email", contact_email);
		params.put("sign", "");
		
		JSONObject response= HttpClientUtils.postFromUrl4Json(Traffic.TRAFFIC_CALL_URI, params);
		
		SysLogger logger = new SysLogger();
		logger.business_code="traffic";
		logger.business_id=userfinance.uid;
		logger.create_user=user.user_id;
		logger.operate_name="流量汇注册";
		logger.create_time=new Date();
		logger.user_name=user.user_name;
		
		StringBuffer sbuf = new StringBuffer();
		if(null==response){
			sbuf.append("调用流量汇注册接口失败！返回response为null");
			logger.operate_desc=sbuf.toString();
			dbService.saveLogger(logger);
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇注册接口失败！");
		}
		
		String status = response.optString("status");
		String code=response.optString("code");
		sbuf.append("[status="+status+"]");
		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
			//注册成功
			
			String company_code = response.optJSONObject("result").getString("company_code");
			sbuf.append("流量汇用户注册成功，【company_code="+company_code+"】");
			userfinance.setTraffic_company_code(company_code);
			//dbService.update4Register(userfinance);
			dbService.saveLogger(logger);
		}else{
			sbuf.append("流量汇用户注册失败，【status="+status+",code="+code+"】");
			dbService.saveLogger(logger);
		}
	    return result;
	}
	
}
